package com.tianma315.admin.device.controller;


import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.device.domain.DeviceTypeDO;
import com.tianma315.admin.device.service.DeviceTypeService;

/**
 * 
 * <pre>
 * 设备类型
 * </pre>
 * <small> 2019-05-22 16:08:48 | Aron</small>
 */
@Controller
@RequestMapping("/device/deviceType")
public class DeviceTypeController extends BaseController{
	@Autowired
	private DeviceTypeService deviceTypeService;
	
	@GetMapping()
	@RequiresPermissions("device:deviceType:deviceType")
	String DeviceType(){
	    return "device/deviceType/deviceType";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("device:deviceType:deviceType")
	public Result<Page<DeviceTypeDO>> list(Integer pageNumber, Integer pageSize, DeviceTypeDO deviceTypeDTO){
		Page<DeviceTypeDO> deviceTypeDOPage = deviceTypeService.getDeviceTypeDOPage(pageNumber, pageSize, deviceTypeDTO);
        return Result.ok(deviceTypeDOPage);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("device:deviceType:add")
	String add(){
	    return "device/deviceType/add";
	}

	@GetMapping("/edit/{deviceTypeId}")
	@RequiresPermissions("device:deviceType:edit")
	String edit(@PathVariable("deviceTypeId") Integer deviceTypeId,Model model){
		DeviceTypeDO deviceType = deviceTypeService.selectById(deviceTypeId);
		model.addAttribute("deviceType", deviceType);
	    return "device/deviceType/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("device:deviceType:add")
	public Result<String> save( DeviceTypeDO deviceType){
		int id = getUserId().intValue();
		deviceType.setCreateBy(id);
		deviceType.setCreateDate(Calendar.getInstance().getTime());
		if(deviceTypeService.addDeviceTypeDO(deviceType)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("device:deviceType:edit")
	public Result<String>  update( DeviceTypeDO deviceType){
		deviceTypeService.updateById(deviceType);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("device:deviceType:remove")
	public Result<String>  remove( Integer deviceTypeId){
		if(deviceTypeService.deleteDeviceTypeDO(deviceTypeId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("device:deviceType:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] deviceTypeIds){
		deviceTypeService.deleteBatchIds(Arrays.asList(deviceTypeIds));
		for (int i = 0; i < deviceTypeIds.length; i++) {
			 deviceTypeService.deleteDeviceTypeDO(deviceTypeIds[i]);
		}
		return Result.ok();
	}

	/**
	 * 获取所有设备类型
	 */
	@GetMapping( "/getDeviceTypeVOList")
	@ResponseBody
	public Result<List<DeviceTypeDO>>  getDeviceTypeVOList(DeviceTypeDO deviceTypeDO){
		List<DeviceTypeDO> deviceTypeDOList = deviceTypeService.getDeviceTypeDOList(deviceTypeDO);
		return Result.ok(deviceTypeDOList);
	}
	
}
